/* 随机组卷 */
import React, { PureComponent } from 'react';
import { Form, Button, Input, Select, Radio, InputNumber, Row, Col, Table, Cascader, List, message } from 'antd';
import { HardLevelSelect, ExamSubjectTreeSelect, RelatedPersonnelextendsTransfer, PaperTemplate } from '../Components/AntComponents'
import { addPaper, getSubjectNum, getTemplateList, getTemplateDetail } from '@/services/paperInfo'
import { isLianxi } from '@/utils/utils'
const layout = {
    labelCol: { span: 2 },
    wrapperCol: { span: 15 },
};
const tailLayout = {
    wrapperCol: { offset: 1 },
};
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props)
        this.state = {
            name: '',
            ruleSubctData: [],
            subject: [],
            template: null,
            complexity: '0',
            total: 0,
            test_type: '2',
            multiple: 0,
            multiple_num: 0,
            multiple_deduct: 0,
            multiple_mistake: 0,
            choices: 0,
            choices_num: 0,
            choices_deduct: 0,
            choices_mistake: 0,
            judge: 0,
            judge_num: 0,
            judge_deduct: 0,
            judge_mistake: 0,
            test_time: 0,
            badStart: 0,
            badEnd: 0,
            sosoStart: 0,
            sosoEnd: 0,
            goodStart: 0,
            goodEnd: 0,
            wonderfulStart: 0,
            wonderfulEnd: 0,
            people: [],
            ruleWay: '1',
            rules: [],
            ruleType: '0',
            ruleSubject: null,
            ruleHardLevel: '0',
            ruleQuestionNum: null,
            ruleSubjectLabel: null,
            ruleHardLevelLabel: null,
            ruleTypeLabel: null,
            loading: false,
            subject_num: '',
            multipleNum: -1, choicesNum: -1, judgeNum: -1
        }
    }
    changeWithTargetValue = (e: any, name: any) => {
        this.setState({
            [name]: e.target.value
        })
    }
    changeValue = (e: any, name: any) => {
        this.setState({
            [name]: e
        })
    }

    changeSubject = (e: any, d: any) => {
        const ruleSubctData = [{ key: e, title: d }];
        this.setState({
            ruleSubctData,
            subject: e
        })
    }
    changeTemplate = (e: any) => {
        this.setState({
            template: e
        })
        getTemplateDetail({ id: e }).then(res => {
            const {
                complexity,
                total,
                test_type,
                multiple_num,
                multiple_score,
                multiple_deduct,
                multiple_mistake,
                choices_num,
                choices_score,
                choices_deduct,
                choices_mistake,
                judge_num,
                judge_score,
                judge_deduct,
                judge_mistake,
                test_time, excellent, fail,
                good, pass } = res;
            const badData = fail ? fail.split(',') : [];
            const sosoData = pass ? pass.split(',') : [];
            const goodData = good ? good.split(',') : '';
            const wonderfulData = excellent ? excellent.split(',') : '';
            this.setState({
                complexity: complexity + '', total, test_type: test_type + '',
                multiple: multiple_num, multiple_num: multiple_score, multiple_deduct, multiple_mistake,
                choices: choices_num, choices_num: choices_score, choices_deduct, choices_mistake,
                judge: judge_num, judge_num: judge_score, judge_deduct, judge_mistake,
                test_time, badStart: badData[0], badEnd: badData[1], sosoStart: sosoData[0], sosoEnd: sosoData[1],
                goodStart: goodData[0], goodEnd: goodData[1], wonderfulStart: wonderfulData[0], wonderfulEnd: wonderfulData[1],

            })
        })
    }
    changeHardLevel = (e: any) => {
        this.setState({
            complexity: e
        })
    }
    changePeople = (e: any) => {
        this.setState({
            people: e
        })
    }
    changeRuleValue = (e: any, d: any, name: any) => {
        this.setState({
            [name]: e,
            [name + 'Label']: d.children
        })
    }

    changeRuleHardLevel = (e: any, d: any) => {
        this.setState({
            ruleHardLevel: e,
            ruleHardLevelLabel: d.children
        })
    }
    addRules = () => {
        const { rules, ruleType, ruleSubject, ruleQuestionNum,
            ruleTypeLabel, ruleHardLevel, ruleHardLevelLabel, ruleSubjectLabel, } = this.state;

        if (!ruleType || !ruleSubject || !ruleQuestionNum) return message.error('请完善规则设置')
        const item = {
            question: ruleType,
            subject: ruleSubject,
            complexity: ruleHardLevel,
            num: ruleQuestionNum,
            ruleHardLevelLabel,
            ruleTypeLabel,
            ruleSubjectLabel,
            key: new Date().getTime()
        };
        this.setState({
            rules: [...rules, item],
            ruleType: null,
            ruleSubject: null,
            ruleHardLevel: null,
            ruleQuestionNum: null,
            ruleSubjectLabel: null,
            ruleHardLevelLabel: null,
            ruleTypeLabel: null,
        })
    }
    deleteRule = (index: number) => () => {
        const { rules } = this.state;
        rules.splice(index, 1)
        this.setState({
            rules: [...rules]
        })
    }
    submitHandle = () => {
        const {
            name, subject, template, complexity, total, test_type, multiple, multiple_num, multiple_deduct, multiple_mistake,
            choices, choices_num, choices_deduct, choices_mistake, judge, judge_num, judge_deduct, judge_mistake, test_time, badStart,
            badEnd, sosoStart, sosoEnd, goodStart, goodEnd, wonderfulStart, wonderfulEnd, people, rules, ruleWay,
        } = this.state;
        let multipleNum = 0;
        let choicesNum = 0;
        let judgeNum = 0;
        let totalNum = 0;
        for (let i = 0; i < rules.length; i++) {
            const item = rules[i];
            if (item.question === '1') {
                multipleNum += item.num
            }
            if (item.question === '2') {
                choicesNum += item.num
            }
            if (item.question === '3') {
                judgeNum += item.num
            }
            totalNum += item.num;
        }
        if (totalNum > Number(multiple) + Number(choices) + Number(judge)) return message.error('抽题规则题数超出试题分布数量')
        if (multipleNum > multiple) return message.error('单选题抽题规则超出试题分布数量')
        if (!isLianxi() && choicesNum > choices) return message.error('多选题抽题规则超出试题分布数量')
        if (judgeNum > judge) return message.error('判断题抽题规则超出试题分布数量')
        const countScore = multiple * multiple_num + choices * choices_num + judge * judge_num;
        if (countScore > total) return message.error('试题分数已超出试卷总分')
        // if (!name || !subject || !total || !test_type || !multiple || !choices || !judge || !test_time ) return message.error('请完善试卷信息')
        this.setState({
            loading: true
        })
        addPaper({
            name, subject, template, complexity, total, test_type, multiple_num: multiple, multiple_score: multiple_num, multiple_deduct, multiple_mistake,
            choices_deduct, choices_mistake, choices_num: choices, choices_score: choices_num, judge_deduct, judge_mistake,
            judge_num: judge, judge_score: judge_num, test_time, fail: badStart + ',' + badEnd, pass: sosoStart + ',' + sosoEnd, good: goodStart + ',' + goodEnd,
            excellent: wonderfulStart + ',' + wonderfulEnd, people, selection_rules: ruleWay, selection_ruless: rules
        }).then(res => {
            this.setState({
                loading: false
            })
            if (res.status === 1) {
                message.success('提交成功')
                this.props.changeTabHandle('3')
            } else {
                message.error('提交失败')
            }
        })
    }
    showSubjectNum = () => {
        const { ruleType, ruleSubject, ruleHardLevel } = this.state;
        if (!ruleType || !ruleSubject || !ruleHardLevel) return message.error('请先选择条件')
        getSubjectNum({ type: ruleType, subject: ruleSubject, complexity: ruleHardLevel }).then(res => {
            this.setState({
                subject_num: res + ''
            })
        })
    }
    render() {
        const { rules, ruleSubctData, ruleWay, test_type, people, total, template,
            complexity, subject, ruleHardLevel, ruleType, ruleQuestionNum, ruleSubject,
            multiple, multiple_num, multiple_deduct, multiple_mistake,
            choices, choices_num, choices_deduct, choices_mistake,
            judge, judge_num, judge_deduct, judge_mistake, test_time, loading, subject_num,
            badStart, badEnd, sosoEnd, sosoStart, goodEnd, goodStart, wonderfulEnd, wonderfulStart
        } = this.state;
        return (
            <div>
                <Form>
                    <Form.Item {...layout} label="考试名称">
                        <Input style={{ width: 200 }} placeholder="请输入考试名称" onChange={(e) => this.changeWithTargetValue(e, 'name')}></Input>
                    </Form.Item>
                    <Form.Item {...layout} label="考试科目">
                        <ExamSubjectTreeSelect onChange={this.changeSubject} value={subject} />
                    </Form.Item>
                    <Form.Item {...layout} label="试卷模板">
                        <PaperTemplate onChange={this.changeTemplate} value={template} />
                    </Form.Item>
                    <Form.Item {...layout} label="难易程度">
                        <HardLevelSelect onChange={this.changeHardLevel} value={complexity} />
                    </Form.Item>
                    <Form.Item {...layout} label="试卷总分">
                        <InputNumber placeholder="请输入试卷总分" style={{ width: 200 }} value={total} onChange={(e) => this.changeValue(e, 'total')} />
                    </Form.Item>
                    <Form.Item {...layout} label="试卷类别">
                        <Radio.Group onChange={(e) => this.changeWithTargetValue(e, 'test_type')} value={test_type}>
                            <Radio value="2">模拟考试试卷</Radio>
                            <Radio value="1">正式考试试卷</Radio>
                        </Radio.Group>
                    </Form.Item>
                    <Form.Item {...layout} label="试题分布">
                        <Row>
                            <Col style={{ marginBottom: 10 }} span={6}>单选题 <InputNumber value={multiple} onChange={(e) => this.changeValue(e, 'multiple')} /> 道</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={multiple_num} onChange={(e) => this.changeValue(e, 'multiple_num')} /> 分&nbsp;&nbsp;(共<b>{multiple * multiple_num}</b>分 )</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={multiple_deduct} onChange={(e) => this.changeValue(e, 'multiple_deduct')} /> 分</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={multiple_mistake} onChange={(e) => this.changeValue(e, 'multiple_mistake')} /> 分</Col>
                        </Row>
                        {
                            !isLianxi() && <Row>
                                <Col style={{ marginBottom: 10 }} span={6}>多选题 <InputNumber value={choices} onChange={(e) => this.changeValue(e, 'choices')} /> 道</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={choices_num} onChange={(e) => this.changeValue(e, 'choices_num')} /> 分&nbsp;&nbsp;(共<b>{choices * choices_num}</b>分 )</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={choices_deduct} onChange={(e) => this.changeValue(e, 'choices_deduct')} /> 分</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={choices_mistake} onChange={(e) => this.changeValue(e, 'choices_mistake')} /> 分</Col>
                            </Row>
                        }

                        <Row>
                            <Col style={{ marginBottom: 10 }} span={6}>判断题 <InputNumber value={judge} onChange={(e) => this.changeValue(e, 'judge')} /> 道</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={judge_num} onChange={(e) => this.changeValue(e, 'judge_num')} /> 分&nbsp;&nbsp;(共<b>{judge * judge_num}</b>分 )</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={judge_deduct} onChange={(e) => this.changeValue(e, 'judge_deduct')} /> 分</Col>
                            <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={judge_mistake} onChange={(e) => this.changeValue(e, 'judge_mistake')} /> 分</Col>
                        </Row>
                    </Form.Item>
                    <Form.Item {...layout} label="考试时长">
                        <InputNumber onChange={(e) => this.changeValue(e, 'test_time')} value={test_time} style={{ width: 200 }} /> 分钟
                    </Form.Item>
                    <Form.Item {...layout} label="分值标准">
                        <Row>
                            <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>不及格</div> <InputNumber value={badStart} onChange={(e) => this.changeValue(e, 'badStart')} /> —— <InputNumber value={badEnd} onChange={(e) => this.changeValue(e, 'badEnd')} /> </Col>
                            <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>及格</div>  <InputNumber value={sosoStart} onChange={(e) => this.changeValue(e, 'sosoStart')} /> —— <InputNumber value={sosoEnd} onChange={(e) => this.changeValue(e, 'sosoEnd')} /></Col>
                            <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>良好</div>  <InputNumber value={goodStart} onChange={(e) => this.changeValue(e, 'goodStart')} /> —— <InputNumber value={goodEnd} onChange={(e) => this.changeValue(e, 'goodEnd')} /></Col>
                            <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>优秀</div>  <InputNumber value={wonderfulStart} onChange={(e) => this.changeValue(e, 'wonderfulStart')} /> —— <InputNumber value={wonderfulEnd} onChange={(e) => this.changeValue(e, 'wonderfulEnd')} /></Col>
                        </Row>
                    </Form.Item>

                    <Form.Item {...layout} label="抽题规则">
                        <Radio.Group value={ruleWay} onChange={(e) => this.changeWithTargetValue(e, 'ruleWay')}>
                            <Radio key="1" value="1">系统自动生成</Radio>
                            <Radio key="2" value="2">人工自定义</Radio>
                        </Radio.Group>
                    </Form.Item>
                    {
                        ruleWay === '2' && (
                            <React.Fragment>
                                <Form.Item {...layout} label="规则设置" help={subject_num ? <div>符合条件的试题共<b style={{ color: "red" }}>{subject_num}</b>道</div> : ''}>
                                    <Select style={{ width: 200 }} placeholder="请选择科目" value={ruleSubject} onChange={(e, d) => this.changeRuleValue(e, d, 'ruleSubject')}>
                                        {
                                            ruleSubctData.map((item: any) => {
                                                return <Select.Option key={item.key} value={item.key}>{item.title}</Select.Option>
                                            })
                                        }
                                    </Select> &nbsp;
                                    <Select style={{ width: 150 }} placeholder="默认所有题型" value={ruleType} onChange={(e, d) => this.changeRuleValue(e, d, 'ruleType')}>
                                        <Select.Option key="0" value="0">全部</Select.Option>
                                        <Select.Option key="1" value="1">单选</Select.Option>
                                        {
                                            !isLianxi() && <Select.Option key="2" value="2">多选</Select.Option>
                                        }

                                        <Select.Option key="3" value="3">判断</Select.Option>
                                    </Select>&nbsp;
                                    <HardLevelSelect onChange={this.changeRuleHardLevel} value={ruleHardLevel} /> &nbsp;
                                    抽试题数 <InputNumber onChange={(e) => this.changeValue(e, 'ruleQuestionNum')} value={ruleQuestionNum} /> 道 &nbsp;
                                    <Button type="primary" onClick={this.addRules} >添加规则</Button>&nbsp;
                                    <Button onClick={this.showSubjectNum}>显示题数</Button>
                                </Form.Item>
                                <Form.Item {...layout} label="规则列表">
                                    <List >
                                        {rules.map((item: any, index: number) => {
                                            return (
                                                <List.Item key={index} actions={[<a key="delete" onClick={this.deleteRule(index)}>删除</a>]}>
                                                    题型：{item.ruleTypeLabel}&nbsp;&nbsp;
                                                    科目：{item.ruleSubjectLabel}&nbsp;&nbsp;
                                                    难易程度：{item.ruleHardLevelLabel}&nbsp;&nbsp;
                                                    抽题数：{item.num}
                                                </List.Item>
                                            )
                                        })}
                                    </List>
                                </Form.Item>
                            </React.Fragment>
                        )
                    }
                    <Form.Item {...layout} label="选择补录人员">
                        <RelatedPersonnelextendsTransfer onChange={this.changePeople} value={people} />
                    </Form.Item>
                    <Form.Item {...tailLayout}>
                        <Button type="primary" onClick={this.submitHandle} loading={loading}>提交</Button>
                    </Form.Item>
                </Form>
            </div>
        )
    }
}
